package biz.gieraltowski.lukships;

import java.awt.image.BufferedImage;
import java.net.URL;
import java.util.HashMap;
import javax.imageio.ImageIO;




public class SpriteCache {
	public HashMap sprites;
	public SpriteCache(){
		sprites = new HashMap();
	}
	
	public BufferedImage getSprite(String path){
		BufferedImage img = (BufferedImage) sprites.get(path);
		if (img == null){
			img = loadImage(path);
			sprites.put(path, img);
		}
		return img;
	}
	
	public BufferedImage loadImage(String path) {
		URL url = null;
		try {
			url = getClass().getResource(path);
			return ImageIO.read(url);
		} catch (Exception e) {
			System.err.println("Błąd przy otwieraniu pliku: " + path);
			e.printStackTrace();
			System.exit(0);
			return null;
		}
	}
	
}
